//
//  SearchDiagnostic.m
//  Cabinet
//
//  Created by Pierre MONTSEGUR on 12/06/11.
//  Copyright 2011 PM. All rights reserved.
//

#import "SearchDiagnostic.h"
#import "Diagnostic.h"


@implementation SearchDiagnostic

@synthesize mDiagnosticsView;
@synthesize mManagedObjectContext;

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.
         mDiagnostics = [[NSMutableArray alloc] init];
    }
    
    return self;
}


- (void)windowDidLoad
{
    [super windowDidLoad];
    
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

- (void)dealloc
{
    if(mDiagnostics != nil)
        [mDiagnostics removeAllObjects];
}

- (NSString*)windowNibName
{
	return @"SearchDiagnostic";
}


- (void)withParentWindow:parentWindow
{
    mSelectedIndex = -1;
	NSWindow* window = [self window];
	
	[NSApp beginSheet:window modalForWindow:parentWindow modalDelegate:nil didEndSelector:nil contextInfo:nil];
	[NSApp runModalForWindow:window];
	// sheet is up now...
	
	[NSApp endSheet:window];
	[window orderOut:self];
	
}

- (IBAction)okAction:sender {
    
    NSLog(@"On OK");
    
    mSelectedIndex = [mDiagnosticsView selectedRow];
    
    mResult = NSOKButton;
    [self closeWindow];
}

- (IBAction)annulerAction:sender {
    
    NSLog(@"On Annuler");
    
    mResult = NSCancelButton;
    [self closeWindow];
}

- (void) closeWindow
{
    [[self mDiagnosticsView] setDataSource:nil];
    [[self window] orderOut:nil];
    [NSApp stopModal];    
}

- (int) returnCode
{
    return mResult;
}

- (void) setDiagnostics:(NSArray*)aDiagnisticsList
{
    [mDiagnostics removeAllObjects];
    
    if(aDiagnisticsList == nil)
        return;
    
    [mDiagnostics addObjectsFromArray:aDiagnisticsList];
    [mDiagnosticsView reloadData];
}

- (Diagnostic*) selectedDiagnotic
{
     return [self getDiagnosticAtIndex:mSelectedIndex];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView{
    return [self count];
}

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex{
    
    if( rowIndex >= [self count])
        return @"";
    
    Diagnostic *diag = [mDiagnostics objectAtIndex:rowIndex];
    if(diag !=nil)
        return [diag Label];
    
    return @"";
}

- (NSInteger) count{
    return [mDiagnostics count];
}

- (Diagnostic*)getDiagnosticAtIndex:(NSInteger)aRowIndex
{
    if(aRowIndex<0) 
        return nil;
    
    if(aRowIndex>=[self count]) 
        return nil;
    
    return [mDiagnostics objectAtIndex:aRowIndex];
}

- (IBAction)supprimerAction:sender
{
    Diagnostic * selectedDiag = [self selectedDiagnotic];
    if(selectedDiag == nil)
        return;
    
    NSAlert * alert = [NSAlert alertWithMessageText:@"Attention"
                                      defaultButton:@"Supprimer"
                                    alternateButton:@"Annuler"
                                        otherButton:nil informativeTextWithFormat:[NSString stringWithFormat:@"%@ sera supprimé définitivement", [selectedDiag Label]] ];
    
    // should we try again?
    if ([alert runModal] == NSOKButton)
    {
        [mDiagnostics removeObject:selectedDiag];
        [mDiagnosticsView reloadData];
        
        [[self mManagedObjectContext] deleteObject:selectedDiag];
        
        NSLog(@"Supprimé !!");
    }
}


@end
